import axios from 'axios'
import 'es6-promise'
import {getStorage} from './redux/login/util'

export const payServerUrl = 'http://gcdd.koncendy.com/api/';
// const serverUrl = 'http://www2.yihangotms.com/';
const imgUrl = 'http://gcdd.koncendy.com/fs/f/'

// export const payServerUrl = 'http://192.168.1.208:8080/';
// export const serverUrl = 'http://192.168.1.159/';
// const imgUrl = 'http://gcdd.koncendy.com/fs/f/'
//const serverUrl = "";
const serverUrl = "http://www2.yihangotms.com/otms2/";
// const imgUrl = 'http://192.168.1.245/fs/f/'

// export const serverUrl = 'http://gcdd.koncendy.com/gcdd-api/';
// const imgUrl = 'http://121.43.183.228:8080/fs/f/'
export {imgUrl,serverUrl}
// http request 拦截器
axios.interceptors.request.use(
  config => {
    if(!getStorage('user').token){  // 判断是否存在token，如果存在的话，则每个http header都加上token
    }else{
      config.headers.token =  JSON.parse(getStorage('user')).token;
    }
    return config;
  },
  err => {
    return Promise.reject(err); 
  });

// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
     // console.log('axios:' + error.response.status);
    }
    return Promise.reject(error.response);   // 返回接口返回的错误信息
  });
export function get(url, data, params) {
  let paramsobj = Object.assign({
    serverUrl : serverUrl
  },params)
  return new Promise((resolve, reject) => {
    axios.get(paramsobj.serverUrl+url, {
      params: data,
      timeout: 60000 
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err)
    })
  })
}

export function post(url, data,params) {
  return new Promise((resolve, reject) => {
    console.log(serverUrl+url)
    axios.post(serverUrl+url, data).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err)
    })
  })
}

export function payPost(url, data) {
  // console.log(serverUrl+url);
   return new Promise((resolve, reject) => {
     axios.post(serverUrl+url, data,{timeout: 60000}
     ).then(res => {
       resolve(res.data)
     }).catch(err => {
       reject(err)
     })
   })
 }

